using System;
using System.Drawing;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Visuals;
using System.Drawing.Drawing2D;

namespace FilteredStatistics.View
{
    public class IconizedSTMenuItem : MenuItem
    {
        public IconizedSTMenuItem()
            : base()
        {
            OwnerDraw = true;
        }

        public IconizedSTMenuItem(string text)
            : base(text)
        {
            OwnerDraw = true;
        }

        public IconizedSTMenuItem(string text, EventHandler onClick)
            : base(text, onClick)
        {
            OwnerDraw = true;
        }

        public IconizedSTMenuItem(string text, MenuItem[] items)
            : base(text, items)
        {
            OwnerDraw = true;
        }

        public IconizedSTMenuItem(string text, EventHandler onClick, Shortcut shortcut)
            : base(text, onClick, shortcut)
        {
            OwnerDraw = true;
        }

        public IconizedSTMenuItem(MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string text, EventHandler onClick, EventHandler onPopup, EventHandler onSelect, MenuItem[] items)
            : base(mergeType, mergeOrder, shortcut, text, onClick, onPopup, onSelect, items)
        {
            OwnerDraw = true;
        }
        public IconizedSTMenuItem(Image icon, Image checkedIcon)
            : this()
        {
            Icon = icon;
            CheckedIcon = checkedIcon;
        }

        public IconizedSTMenuItem(string text, Image icon, Image checkedIcon)
            : this(text)
        {
            Icon = icon;
            CheckedIcon = checkedIcon;
        }

        public IconizedSTMenuItem(string text, EventHandler onClick, Image icon, Image checkedIcon)
            : this(text, onClick)
        {
            Icon = icon;
            CheckedIcon = checkedIcon;
        }

        public IconizedSTMenuItem(string text, MenuItem[] items, Image icon, Image checkedIcon)
            : this(text, items)
        {
            Icon = icon;
            CheckedIcon = checkedIcon;
        }

        public IconizedSTMenuItem(string text, EventHandler onClick, Shortcut shortcut, Image icon, Image checkedIcon)
            : this(text, onClick, shortcut)
        {
            Icon = icon;
            CheckedIcon = checkedIcon;
        }

        public IconizedSTMenuItem(MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string text, EventHandler onClick, EventHandler onPopup, EventHandler onSelect, MenuItem[] items, Image icon, Image checkedIcon)
            : this(mergeType, mergeOrder, shortcut, text, onClick, onPopup, onSelect, items)
        {
            Icon = icon;
            CheckedIcon = checkedIcon;
        }
        
        ~IconizedSTMenuItem()
        {
            m_BackgroundColor.Dispose();
            m_SeperatorColor.Dispose();
            m_HighlightColor.Dispose();
            m_BorderColor.Dispose();
            m_TextColor.Dispose();
        }
    
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            LinearGradientBrush menuBarBrush = new LinearGradientBrush(new Rectangle(e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height + 1), PluginMain.GetApplication().VisualTheme.Control, PluginMain.GetApplication().VisualTheme.Window, LinearGradientMode.Horizontal);

            e.Graphics.FillRectangle(m_BackgroundColor, e.Bounds.X, e.Bounds.Y, e.Bounds.Width+1, e.Bounds.Height+1);
            e.Graphics.FillRectangle(menuBarBrush, e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height + 1);

            if (base.Text == "-")
            {
                e.Graphics.FillRectangle(m_SeperatorColor, e.Bounds.X + 20,
                           e.Bounds.Y + 2, e.Bounds.Width - 20, e.Bounds.Height - 2);
            }
            else
            {
                if ((e.State & DrawItemState.Selected) != 0)
                {
                    e.Graphics.FillRectangle(m_HighlightColor, e.Bounds);
                    e.Graphics.DrawRectangle(m_BorderColor, e.Bounds.X,
                               e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
                }

                if (Checked && CheckedIcon != null)
                {
                    Rectangle rIcon = new Rectangle(e.Bounds.X + 1, e.Bounds.Y + 3, 16, 16);
                    e.Graphics.DrawImageUnscaled(CheckedIcon, rIcon);
                }
                else if (!Checked && Icon != null)
                {
                    Rectangle rIcon = new Rectangle(e.Bounds.X + 1, e.Bounds.Y + 3, 16, 16);
                    e.Graphics.DrawImageUnscaled(Icon, rIcon);
                }

                e.Graphics.DrawString(base.Text, SystemFonts.MenuFont, m_TextColor,
                         e.Bounds.X + 22, e.Bounds.Y + 3);
            }

            menuBarBrush.Dispose();
        }

        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            if (base.Text == "-")
            {
                e.ItemHeight = 4;
            }
            else
            {
                e.ItemHeight = 22;
                e.ItemWidth = Convert.ToInt32(e.Graphics.MeasureString(this.Text,
                                                                  SystemFonts.MenuFont).Width) + 40;
            }
        }

        public Image Icon
        {
            get { return m_Icon; }
            set
            {
                if (Icon != value)
                {
                    m_Icon = value;
                }
            }
        }

        public Image CheckedIcon
        {
            get { return m_CheckedIcon; }
            set
            {
                if (Icon != value)
                {
                    if (value == null)
                    {
                        m_CheckedIcon = Resources.Resources.CheckedMenuItem;
                    }
                    else
                    {
                        m_CheckedIcon = value;
                    }
                }
            }
        }

        private Image m_Icon = null;
        private Image m_CheckedIcon = Resources.Resources.CheckedMenuItem;
        private SolidBrush m_BackgroundColor = new SolidBrush(PluginMain.GetApplication().VisualTheme.Window);
        private SolidBrush m_SeperatorColor = new SolidBrush(PluginMain.GetApplication().VisualTheme.Control);
        private SolidBrush m_HighlightColor = new SolidBrush(PluginMain.GetApplication().VisualTheme.Highlight);
        private Pen m_BorderColor = new Pen(PluginMain.GetApplication().VisualTheme.Control);
        private SolidBrush m_TextColor = new SolidBrush(PluginMain.GetApplication().VisualTheme.ControlText);
    }
}
